<template> 
  <div class="app-container">
    <el-page-header @back="goBack" content="用户添加">
      <template #title>
        <span class="page-title">
          <i class="el-icon-user-solid" /> 用户管理
        </span>
      </template>
    </el-page-header>

    <el-card class="form-card">
      <div class="el-card__header">
        <h3 class="form-title" style="font-size: 14px; text-align: center">
          <i class="el-icon-edit" /> 新建用户信息
        </h3>
      </div>

      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="form.username" placeholder="请输入用户名" />
            </el-form-item>

            <el-form-item label="密码" prop="password">
              <el-input v-model="form.password" placeholder="请输入密码" show-password />
            </el-form-item>

            <el-form-item label="邮箱">
              <el-input v-model="form.email"></el-input>
            </el-form-item>
            <el-form-item label="手机号">
              <el-input v-model="form.phone" ></el-input>
            </el-form-item>
            <el-form-item label="性别">
              <el-radio-group v-model="form.genderCode">
                <el-radio label="1">男</el-radio>
                <el-radio label="2">女</el-radio>
                <el-radio label="3">保密</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="个人简介">
              <el-input
                  type="textarea"
                  v-model="form.bio"
                  :rows="3"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item class="form-actions">
          <el-button @click="resetForm">重置</el-button>
          <el-button type="primary" @click="submitForm">立即创建</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from '@/utils/request';

export default {
  data() {
    return {
      form: {
        username: '',
        password: '',
        email:'' ,
        phone:'',
        genderCode:'',
        bio:''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '密码长度6-16位', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    goBack() {
      this.$router.go(-1)
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          axios.post('/user/saveOrUpdate', this.form)
              .then(response => {
                if (response.data.code === 200) {
                  this.$message.success('用户创建成功');
                  this.resetForm();
                  this.$router.push('/apiusers/list');
                } else {
                  this.$message.error(response.data.message || '操作失败');
                }
              })
              .catch(error => {
                console.error('请求错误:', error);
                this.$message.error(error.response?.data?.message || '服务器错误');
              });
        } else {
          this.$message.warning('请填写完整的表单信息');
        }
      });
    },
    resetForm() {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style scoped>
.form-card {
  width: 600px;
  margin: 20px auto 0;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.el-card__header {
  padding: 12px 20px;
}

.el-form-item {
  margin-bottom: 18px;
}

.el-row {
  margin: 0 -10px;
}

.el-col {
  padding: 0 10px;
}

.el-card__header {
  border-bottom: 1px solid #ebeef5;
  padding: 18px 20px;
}

.form-title {
  font-size: 16px;
  color: #303133;
  display: flex;
  align-items: center;
}

.form-title i {
  margin-right: 8px;
  color: #409EFF;
}

.el-form-item {
  margin-bottom: 22px;
}

.el-form-item :deep(.el-form-item__label) {
  color: #606266;
  font-weight: 500;
}

.form-actions {
  margin-top: 30px;
  display: flex;
  justify-content: center;
  gap: 20px;
}
</style>

